Ext.define('YXMES.sqcm.controller.OCVStandardController', {
	extend : 'Ext.app.Controller',
	refs : [ {
		selector : 'sqcm_ocvstandard',
		ref : 'sqcmocvstandard'
	} ],
	requires : [ 'YXMES.sqcm.store.OcvStdListStore', 'YXMES.sqcm.store.ProductMaterialStore', 'YXMES.sqcm.store.OCVProcessStore', 'YXMES.sqcm.store.OCVStdTypeStore' ],
	stores : [],
	models : [],
	views : [ 'YXMES.sqcm.view.OCVStandardView' ],
	subcontrollers : [  ],
	_init : false,
	init : function() {
		if (this._init)
			return;
		this._init = true;
		this.control({
			'sqcm_ocvstandard' : {
				afterrender : function() {
					return this.handle(this.onAfterrender, arguments);
				},
				destroy : function() {
					return this.handle(this.onDestroy, arguments);
				}
			},
			'sqcm_ocvstandard #btnSearch' : {
				click : function() {
					return this.handle(this.onBtnSearchClick, arguments);
				}
			},
			'sqcm_ocvstandard #btnSave' : {
				click : function() {
					return this.handle(this.onBtnSaveClick, arguments);
				}
			},
			'sqcm_ocvstandard #btnDel' : {
				click : function() {
					return this.handle(this.onBtnDelClick, arguments);
				}
			},
			'sqcm_ocvstandard #btnReset' : {
				click : function() {
					return this.handle(this.onBtnResetClick, arguments);
				}
			},
			'sqcm_ocvstandard #grdList' : {
				itemclick : function() {
					return this.handle(this.onGrdListItemclick, arguments);
				}
			}
		});
		if (!Ext.isEmpty(this.subcontrollers)) {
			Ext.each(this.subcontrollers, function(name) {
				this.getController(name).init();
			}, this);
		}
		// User definied init
	},
	mainView : undefined,
	getMainView : function() {
		return this.mainView;
	},
	getScopeView : function() {
		return this.getMainView().scopeView || {};
	},
	getInput : function() {
		return this.getScopeView().input || {};
	},
	setInput : function(data) {
		this.getMainView().input = data;
	},
	getOutput : function() {
		return this.getMainView().output || {};
	},
	setOutput : function(data) {
		this.getScopeView().output = data;
	},
	handle : function(func, args) {
		var item = args && args.length > 0 && Ext.typeOf(args[0]) === 'object' ? args[0] : undefined;
		if (item) {
			try {
				this.mainView = (item.xtype == 'sqcm_ocvstandard' ? item : item.up('sqcm_ocvstandard'));
			} catch(e) {
			}
		}
		try {
			return func.apply(this, args);
		} finally {
		}
	},
	getItem : function(itemId) {
		return itemId===this.getMainView().getItemId()?this.getMainView():this.getMainView().down('#' + itemId);
	},
	getStore : function(name) {
		if (name == 'OcvStdListStore')
			return this.getMainView().SqcmViewOcvStandardServiceStore;
		if (name == 'ProductMaterialStore')
			return this.getMainView().SbasViewProductServiceStore;
		if (name == 'OCVProcessStore')
			return this.getMainView().BasViewCommonDataSetupDetailServiceStore;
		if (name == 'OCVStdTypeStore')
			return this.getMainView().BasViewCommonDataSetupDetailServiceStore2;
		if (name == 'StdStatusStore')
			return this.getMainView().stdStatusStoreStore;
		return undefined;
	},
	bind : function(view) {
		view = (view ? view : this.getMainView());
		view.SqcmViewOcvStandardServiceStore = Ext.create('YXMES.sqcm.store.OcvStdListStore', {
			pageSize : 30
		});
		view.SbasViewProductServiceStore = Ext.create('YXMES.sqcm.store.ProductMaterialStore', {
			pageSize : 30,
			autoDestory : true,
			autoLoad : true
		});
		view.BasViewCommonDataSetupDetailServiceStore = Ext.create('YXMES.sqcm.store.OCVProcessStore', {
			pageSize : 30
		});
		view.BasViewCommonDataSetupDetailServiceStore2 = Ext.create('YXMES.sqcm.store.OCVStdTypeStore', {
			pageSize : 30,
			autoDestory : true
		});
		view.stdStatusStoreStore = Ext.create('Ext.data.ArrayStore', {
			pageSize : 30,
			autoLoad : true,
			fields : [ {
				name : 'k',
				type : 'string'
			}, {
				name : 'v',
				type : 'string'
			} ],
			data : [
				[ 'Y', 'Y' ],
				[ 'N', 'N' ]
			]
		});
		view.down('#grdList').reconfigure(view.SqcmViewOcvStandardServiceStore);
		view.down('#bar3').bindStore(view.SqcmViewOcvStandardServiceStore);
		view.down('#cdvMatCode').bindStore(view.SbasViewProductServiceStore);
		view.down('#cmbProcess').bindStore(view.BasViewCommonDataSetupDetailServiceStore);
		view.down('#cmbStdType').bindStore(view.BasViewCommonDataSetupDetailServiceStore2);
		view.down('#cmbStdStatus').bindStore(view.stdStatusStoreStore);
	},
	onAfterrender : function(view, eOpts) {
		this.bind();
		// User defined afterrender
	},
	onDestroy : function(view, eOpts) {
		// User defined destory
		var view = this.getMainView();
		if (view.SqcmViewOcvStandardServiceStore) {
			view.SqcmViewOcvStandardServiceStore.destroyStore();
			view.SqcmViewOcvStandardServiceStore = undefined;
		}
		if (view.SbasViewProductServiceStore) {
			view.SbasViewProductServiceStore.destroyStore();
			view.SbasViewProductServiceStore = undefined;
		}
		if (view.BasViewCommonDataSetupDetailServiceStore) {
			view.BasViewCommonDataSetupDetailServiceStore.destroyStore();
			view.BasViewCommonDataSetupDetailServiceStore = undefined;
		}
		if (view.BasViewCommonDataSetupDetailServiceStore2) {
			view.BasViewCommonDataSetupDetailServiceStore2.destroyStore();
			view.BasViewCommonDataSetupDetailServiceStore2 = undefined;
		}
		if (view.stdStatusStoreStore) {
			view.stdStatusStoreStore.destroyStore();
			view.stdStatusStoreStore = undefined;
		}
		if (this.getMainView().input)
			this.getMainView().input = undefined;
		if (this.getMainView().output)
			this.getMainView().output = undefined;
	},
	onBtnSearchClick : function(button, e, eOpts) {
		this.loadOcvStdListStore(this.getItem('frmSearch').getValues());
	},
	onBtnSaveClick : function(button, e, eOpts) {
		if(!this.getItem('frmOperation').isValid()){
		    return;
		}
		
		this.saveData();
	},
	onBtnDelClick : function(button, e, eOpts) {
		if(this.getItem('hidId').getValue()===""){
		    MessageBox.alert('删除','请选择一条记录。');
		    return;
		}
		
		this.saveData('D');
	},
	onBtnResetClick : function(button, e, eOpts) {
		this.getItem('frmOperation').getForm().reset();
	},
	onGrdListItemclick : function(dataview, record, item, index, e, eOpts) {
		this.getItem('frmOperation').loadRecord(record);
	},
	loadOcvStdListStore : function(params, options) {
		params = Ext.applyIf(params||{}, {
			// Default params
		});
		
		var store = this.getStore('OcvStdListStore');
		if(!store) {
			return false;
		}
		
		store.getProxy().extraParams = params;
		if(store.loadPage){
			store.loadPage(1, options);
		}else{
			store.load(options);
		}
	},
	saveData : function(pStatus) {
		var pms = this.getItem('frmOperation').getValues();
		if (!Ext.isEmpty(pStatus)) {
		    pms.stdStatus = pStatus;
		}
		
		var btnSave = this.getItem('btnSave');
		var btnDel = this.getItem('btnDel');
		var saveDisabled = btnSave.disabled;
		var delDisabled = btnDel.disabled;
		
		var me = this;
		Ext.Msg.show({
		    title: T.label.save,
		    msg: T.msg.are_you_sure_to_save_data,
		    icon: Ext.Msg.QUESTION,
		    buttons: Ext.Msg.YESNO,
		    fn: function(btnId) {
		        if ('yes' == btnId) {
		            btnSave.setDisabled(true);
		            btnDel.setDisabled(true);
		
		            YX.ZbusRpc.request({
		                module: 'SqcmSaveOcvStandardService',
		                showSuccess: true,
		                params: {
		                    ocvStd: pms
		                },
		                success: function(rsp) {
		                    me.getItem('frmOperation').getForm().reset();
		                    me.loadOcvStdListStore();
		                },
		                callback: function() {
		                    btnSave.setDisabled(saveDisabled);
		                    btnDel.setDisabled(delDisabled);
		                }
		            });
		        }
		    }
		});
	},
	loadProductMaterialStore : function(params, options) {
		params = Ext.applyIf(params||{}, {
			// Default params
		});
		
		var store = this.getStore('ProductMaterialStore');
		if(!store) {
			return false;
		}
		
		store.getProxy().extraParams = params;
		if(store.loadPage){
			store.loadPage(1, options);
		}else{
			store.load(options);
		}
	},
	loadOCVProcessStore : function(params, options) {
		params = Ext.applyIf(params||{}, {
			// Default params
		});
		
		var store = this.getStore('OCVProcessStore');
		if(!store) {
			return false;
		}
		
		store.getProxy().extraParams = params;
		if(store.loadPage){
			store.loadPage(1, options);
		}else{
			store.load(options);
		}
	},
	loadOCVStdTypeStore : function(params, options) {
		params = Ext.applyIf(params||{}, {
			// Default params
		});
		
		var store = this.getStore('OCVStdTypeStore');
		if(!store) {
			return false;
		}
		
		store.getProxy().extraParams = params;
		if(store.loadPage){
			store.loadPage(1, options);
		}else{
			store.load(options);
		}
	},
	loadStdStatusStore : function(params, options) {
		params = Ext.applyIf(params||{}, {
			// Default params
		});
		
		var store = this.getStore('StdStatusStore');
		if(!store) {
			return false;
		}
		
		store.getProxy().extraParams = params;
		if(store.loadPage){
			store.loadPage(1, options);
		}else{
			store.load(options);
		}
	}
});